export default class Storage {
  // 存储数据
  static setItem(key: string, value: string | object): void {
    try {
      const serializedValue = JSON.stringify(value)
      localStorage.setItem(key, serializedValue)
    } catch (error) {
      console.error(`Error setting item: ${key}`, error)
    }
  }

  // 获取数据
  static getItem<T>(key: string): T | null {
    try {
      const serializedValue = localStorage.getItem(key)
      if (serializedValue === null) {
        return null
      }
      return JSON.parse(serializedValue) as T
    } catch (error) {
      console.error(`Error getting item: ${key}`, error)
      return null
    }
  }

  // 删除数据
  static removeItem(key: string): void {
    try {
      localStorage.removeItem(key)
    } catch (error) {
      console.error(`Error removing item: ${key}`, error)
    }
  }

  // 清空存储
  static clear(): void {
    try {
      localStorage.clear()
    } catch (error) {
      console.error('Error clearing local storage', error)
    }
  }
}
